package com.vitas.coin.ui.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import com.snap.base.util.BaseUtil.postEvent
import com.snap.util.toast
import com.vitas.coin.data.bean.ChildrenVoiceBean
import com.vitas.coin.data.bean.MusicType
import com.vitas.coin.data.event.ListEvent
import com.vitas.coin.databinding.DialogAddToPlaylistBinding
import com.vitas.coin.db.ListDao
import com.vitas.coin.db.TypeDao
import com.vitas.coin.ui.adapter.TypeListAdapter
import com.vitas.coin.util.Utils
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

class Add2ListDlg : BottomDlg(), DetailJumper {
    lateinit var binding: DialogAddToPlaylistBinding
    val gson = Gson()
    var list: ArrayList<MusicType>? = null
    var adapter: TypeListAdapter? = null
    var child: ChildrenVoiceBean? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DialogAddToPlaylistBinding.inflate(inflater, container, false)
        binding.ivClose.setOnClickListener { dismiss() }
        binding.rvList.layoutManager =
            LinearLayoutManager(requireActivity(), RecyclerView.VERTICAL, false)
        binding.tvCreate.setOnClickListener {
            NewListDlg().setOnDismissListener<NewListDlg> { isOk, value ->
                if (isOk && value is String) {
                    list?.let {
                        var type = MusicType(value.toString())
                        GlobalScope.launch {
                            TypeDao().insert(type)
                            h.post {
                                it.add(type)
                                adapter?.notifyItemInserted(it.size - 1)
                                postEvent(ListEvent())
                            }
                        }
                    }
                }
            }.show(childFragmentManager, "")
        }


        getData()
        return binding!!.getRoot()
    }

    open fun setChildData(data: ChildrenVoiceBean): Add2ListDlg {
        child = data
        return this
    }

    fun getData() {
        GlobalScope.launch {
            list = Utils.getList()
            h.post {
                adapter = TypeListAdapter(this@Add2ListDlg, requireActivity(), list)
                binding.rvList.adapter = adapter
            }
        }
    }

    override fun toList(page: Int) {
        if (list == null || list!!.size <= page || child == null) {
            return
        }
        var tmp = list!![page]
        if (tmp.list == null) {
            tmp.list = ArrayList()
        }
        for (item in tmp.list!!) {
            if (item.id == child!!.id) {
                toast("铃单中已包含该铃声")
                return
            }
        }
        tmp.list!!.add(child!!)
        adapter?.notifyItemChanged(page)
        GlobalScope.launch {
            ListDao(tmp.id).insert(child!!)
            h.post {
                postEvent(ListEvent())
            }
        }
    }
}
